package com.right.dao.impl;

import java.sql.Connection;
import java.util.List;

import com.Tools.DBTools;
import com.Tools.PageData;
import com.right.dao.OrderdetailDao;
import com.right.entity.Orderdetail;

public class OrderdetailDaoImp implements OrderdetailDao {
   /**
    * 增加方法
    */
	@Override
	public boolean addorderdetail(Orderdetail orderdetail) {
	if (orderdetail!=null) {
		String sql="insert into orderdetail values(null,?,?,?,?)";
		    int n=DBTools.exUpdate(sql,orderdetail.getOrderid(),orderdetail.getCount(),orderdetail.getFoods().getFoodsname(),
		    		orderdetail.getTotal());
		    return n>0;
	}
		return false;
	}
	/**
	 * 增加方法(事务)
	 * @param conn 连接
	 * @param orderdetail 订单详情
	 * @return n=0 添加失败;n>0 添加成功
	 */
	@Override
	public int addorderdetail(Connection conn,Orderdetail orderdetail) {

		String sql="insert into orderdetail values(null,?,?,?,?)";
		int n = 0;
		try {
			n=DBTools.exUpdate(sql,conn,orderdetail.getOrderid(),orderdetail.getCount(),orderdetail.getFoods().getFoodsname(),
		    		orderdetail.getTotal());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		    

		return n;
	}
	
   /**
    * 删除方法
    */
	@Override
	public boolean delorderdetail(int oid) {
		String sql="delete from orderdetail where oid=?";
		     int n=DBTools.exUpdate(sql, oid);
		 
		return n>0;
	}
  /**
   * 修改方法
   */
	@Override
	public boolean updateorderdetail(Orderdetail orderdetail) {
		if (orderdetail!=null) {
			 String sql="update orderdetail set orderid=?,count=?,foodsname=?,price=? where oid=? ";
		 int n=DBTools.exUpdate(sql, orderdetail.getOrderid(),orderdetail.getCount(),orderdetail.getFoods().getFoodsname(),
			    		orderdetail.getTotal(),orderdetail.getOid());
		 return n>0;
		}
		return false;
	}
   /**
    * 查询
    */
	@Override
	public List<Orderdetail> getOrderdetails(String orderid) {
		String sql="select oid,foodsname,count,price,total from orderdetail where orderid = ?  ";
	  List<Orderdetail> list=(List<Orderdetail>) DBTools.exQuery(sql, Orderdetail.class, orderid);
		return list;
	}
  /**
   * 分页查询
   */
	@Override
	public PageData<Orderdetail> Orderdetailbypage(int page, int pageSize, String keywords) {
		String sql="select*from orderdetail where oid like ?";
		    PageData<Orderdetail> pd=DBTools.exQueryByPage(sql, Orderdetail.class, page, pageSize, "%"+keywords+"%");
		return pd;
	}

}
